package com.movitech.mobile.cz.base.purchase.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.modules.config.PurchaseCommonEnum;
import com.movitech.mobile.cz.base.purchase.entity.PSpOverallPlanDetail;
import com.movitech.mobile.cz.base.purchase.mapper.PSpOverallPlanDetailMapper;
import com.movitech.mobile.cz.base.purchase.mapper.ext.ExtPSpOverallPlanDetailMapper;
import com.movitech.mobile.cz.base.purchase.service.PSpOverallPlanDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 战略采购年度总规划-详情 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-03-15
 */
@Service
public class PSpOverallPlanDetailServiceImpl extends ServiceImpl<PSpOverallPlanDetailMapper, PSpOverallPlanDetail> implements PSpOverallPlanDetailService {
    @Autowired
    private ExtPSpOverallPlanDetailMapper extPSpOverallPlanDetailMapper;
    @Override
    public boolean addOrReduceurchasedAmount(Double amount, String id) {
        extPSpOverallPlanDetailMapper.addOrReduceurchasedAmount(amount,id);
        return true;
    }

    @Override
    public List<PSpOverallPlanDetail> selectByEntity(PSpOverallPlanDetail pSpOverallPlanDetail) {
        EntityWrapper entityWrapper = new EntityWrapper();
        pSpOverallPlanDetail.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
        entityWrapper.setEntity(pSpOverallPlanDetail);

        return selectList(entityWrapper);
    }

    @Override
    public void updatePlanStatusByYear(Integer oldStatus, Integer newStatus, Integer year) {
        PSpOverallPlanDetail where = new PSpOverallPlanDetail();
        where.setPlanStatus(oldStatus);
        where.setPlanYear(year);
        where.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
        PSpOverallPlanDetail update = new PSpOverallPlanDetail();
        update.setPlanStatus(newStatus);
        update(update,new EntityWrapper<>(where));
    }

    @Override
    public void updatePlanStatusByYearNotIn(Integer oldStatus, Integer newStatus, Integer year) {
        PSpOverallPlanDetail where = new PSpOverallPlanDetail();
        where.setPlanYear(year);
        where.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
        PSpOverallPlanDetail update = new PSpOverallPlanDetail();
        update.setPlanStatus(newStatus);
        update(update,new EntityWrapper<>(where).notIn("plan_status", PurchaseCommonEnum.P_OVER_PLAN_STATUS_1.getCode()));
    }
}
